package xin.sunzy.spring.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xin.sunzy.spring.model.LearnResource;
import xin.sunzy.spring.model.PageInfo;
import xin.sunzy.spring.service.LearnService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description: [描述文本][optional]
 * email: <a href="sunzhenyu@terminus.io">孙振宇</a>
 * author: ginkgo
 * date: 2018/3/11
 */
@Slf4j
@RestController
@RequestMapping("/learn")
public class LearnController {

    @Autowired
    private LearnService learnService;

    @GetMapping(value = "/query-learn-list", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object queryLearnList(
            @RequestParam("page") Integer page,
            @RequestParam("rows") Integer rows,
            @RequestParam(value = "author", required = false) String author,
            @RequestParam(value = "title", required = false) String title) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("rows", rows);
        params.put("author", author);
        params.put("title", title);
        List<LearnResource> learnList = null;
        try {
            learnList = learnService.queryLearnResourceList(params);
        } catch (Exception e) {
            log.error("query.learn.list,cause:{}", e.getMessage());
        }
        return new PageInfo<>(learnList.size(), learnList);
    }
}
